In [1]:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# Optional for interactive
# %matplotlib notebook (watch video for full details)
In [2]:
mcdon = pd.read_csv('mcdonalds.csv',
index_col = 'Date',
parse_dates = True)
In [3]:
mcdon.head()
Out[3]:
In [4]:
# Not Good!
mcdon.plot()
Out[4]:
In [5]:
mcdon['Adj. Close'].plot()
Out[5]:
In [6]:
mcdon['Adj. Volume'].plot()
Out[6]:
In [7]:
mcdon['Adj. Close'].plot(figsize = (12, 8))
Out[7]:
In [8]:
mcdon['Adj. Close'].plot(figsize = (12, 8))
plt.ylabel('Close Price')
plt.xlabel('Overwrite Date Index')
plt.title('Mcdonalds')
Out[8]:
In [9]:
mcdon['Adj. Close'].plot(figsize = (12,8),
title = 'Pandas Title')
Out[9]:
In [10]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01', '2009-01-01'])
Out[10]:
In [11]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01','2009-01-01'],
ylim = [0,50])
Out[11]:
In [12]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01','2007-05-01'],
ylim = [0,40],
ls = '--',
c = 'r')
Out[12]:
In [13]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
In [14]:
mcdon['Adj. Close'].plot(xlim = ['2007-01-01','2007-05-01'],
ylim = [0,40])
Out[14]:
In [15]:
idx = mcdon.loc['2007-01-01':'2007-05-01'].index
stock = mcdon.loc['2007-01-01':'2007-05-01']['Adj. Close']
In [16]:
idx
Out[16]:
In [17]:
stock
Out[17]:
In [18]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
plt.tight_layout()
plt.show()
In [19]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
In [20]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
In [21]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('%b\n%Y'))
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
In [22]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n\n\n%Y--%B'))
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
In [23]:
fig, ax = plt.subplots()
ax.plot_date(idx, stock,'-')
# Major Axis
ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n%Y--%B'))
# Minor Axis
ax.xaxis.set_minor_locator(dates.WeekdayLocator())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%d'))
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()
In [24]:
fig, ax = plt.subplots(figsize=(10,8))
ax.plot_date(idx, stock,'-')
# Major Axis
ax.xaxis.set_major_locator(dates.WeekdayLocator(byweekday=1))
ax.xaxis.set_major_formatter(dates.DateFormatter('%B-%d-%a'))
# Grids
ax.yaxis.grid(True)
ax.xaxis.grid(True)
fig.autofmt_xdate() # Auto fixes the overlap!
plt.tight_layout()
plt.show()